class Solution {
public:
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    bool arr[101][101];
    int nearestExit(vector<vector<char>>& m, vector<int>& e) {
        queue<pair<int,int>> q;
        q.push({e[0],e[1]});
        arr[e[0]][e[1]]=true;
        int ret=0;
        int a=m.size();
        int b=m[0].size();
        while(q.size())
        {
            ret++;
            int sz=q.size();
            for(int i=0;i<sz;++i)
            {
                auto [x,y]=q.front();
                q.pop();
                for(int j=0;j<4;++j)
                {
                    int x1=x+dx[j];
                    int y1=y+dy[j];
                    if(x1>=0&&x1<a&&y1>=0&&
                    y1<b&&m[x1][y1]=='.'&&arr[x1][y1]==false)
                    {
                        if(x1==0||x1==a-1||y1==0||y1==b-1)
                        {
                            return ret;
                        }
                        q.push({x1,y1});
                        arr[x1][y1]=true;
                    }
                }
            }
        }
        return -1;
    }
};
